home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / blit / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-27  |  4.5 KB  |  185 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: bitmap.c,v 4.2 88/07/07 09:04:29 sau Exp $
  9.     $Source: /tmp/mgrsrc/src/blit/RCS/bitmap.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/src/blit/RCS/bitmap.c,v $$Revision: 4.2 $";
  12.  
  13. /*  SUN-2  and SUN-3 bitblit code */
  14.  
  15. #ifdef sun
  16. #  include <sys/ioctl.h>
  17. #  include <sun/fbio.h>
  18. #  include <sys/file.h>
  19. #  include <sys/mman.h>
  20. #endif
  21. #include <stdio.h>
  22. #include "bitmap.h"
  23.  
  24. int bit_debug = 1;
  25. static int _s_start;
  26. static _s_len;
  27.  
  28. /* open the screen; it looks like memory */
  29.  
  30. BITMAP *
  31. bit_open(name)
  32. char *name;            /* name of frame buffer */
  33. {
  34.    BITMAP *result = BIT_NULL;
  35. #ifdef sun
  36.    int fd;
  37.    char *malloc();
  38.    register DATA addr;
  39.    struct fbtype buff;
  40.    int pagesize;
  41.  
  42.    Bprintf(1) ("bit_open:(%s)\n", name);
  43.  
  44.    /* open the SUN screen */
  45.  
  46.    if ((fd = open(name, O_RDWR)) < 0)
  47.       return (BIT_NULL);
  48.  
  49.    /* get the frame buffer size */
  50.  
  51.    if (ioctl(fd, FBIOGTYPE, &buff) < 0)
  52.       return (BIT_NULL);
  53.  
  54.    /* malloc space for frame buffer */
  55.  
  56.    pagesize = getpagesize();
  57.    if ((_s_start = (int) malloc(buff.fb_size + pagesize)) == 0)
  58.       return (BIT_NULL);
  59.  
  60.    /* align space on a page boundary */
  61.  
  62.    buff.fb_size = (buff.fb_size+pagesize-1) &~ (pagesize-1);
  63.    addr = (DATA ) ((_s_start + pagesize - 1) & ~(pagesize - 1));
  64.  
  65.    /* map the frame buffer into malloc'd space */
  66.  
  67. #ifdef _MAP_NEW        /* New semantics for mmap in Sun release 4.0 */
  68.    addr = (DATA) mmap(addr, _s_len=buff.fb_size,
  69.                          PROT_READ|PROT_WRITE, _MAP_NEW|MAP_SHARED, fd, 0);
  70.    if ((int)addr == -1)
  71.       return (BIT_NULL);
  72. #else
  73.    if (mmap(addr, _s_len = buff.fb_size, PROT_WRITE, MAP_SHARED, fd, 0) < 0)
  74.       return (BIT_NULL);
  75. #endif
  76.  
  77.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  78.       return (BIT_NULL);
  79.  
  80.    result->primary = result;
  81.    result->data = addr;
  82.    result->x0 = 0,
  83.       result->y0 = 0,
  84.       result->wide = buff.fb_width;
  85.    result->high = buff.fb_height;
  86.    result->type = _SCREEN;
  87.    Bprintf(2) ("  O.K.(0x%lx)\n (%d x %x)",
  88.            (long) result->data, result->wide, result->high);
  89. #endif
  90.    return (result);
  91. }
  92.  
  93. /* destroy a bitmap, free up space */
  94.  
  95. int
  96. bit_destroy(bitmap)
  97. BITMAP *bitmap;
  98. {
  99.    Bprintf(1) ("bit_destroy:\n");
  100.    if (bitmap == (BITMAP *) 0)
  101.       return (-1);
  102.    if (IS_MEMORY(bitmap) && IS_PRIMARY(bitmap))
  103.       free(bitmap->data);
  104.    else if (IS_SCREEN(bitmap) && IS_PRIMARY(bitmap)) {
  105.       munmap(BIT_DATA(bitmap), _s_len);
  106.       free(_s_start);
  107.    }
  108.    free(bitmap);
  109.    return (0);
  110. }
  111.  
  112. /* create a bitmap as a sub-rectangle of another bitmap */
  113.  
  114. BITMAP *
  115. bit_create(map, x, y, wide, high)
  116. BITMAP *map;
  117. int x, y, wide, high;
  118. {
  119.    char *malloc();
  120.    register BITMAP *result;
  121.  
  122.    Bprintf(1) ("bit_create:\n");
  123.    if (x + wide > map->wide)
  124.       wide = map->wide - x;
  125.    if (y + high > map->high)
  126.       high = map->high - y;
  127.    if (wide < 1 || high < 1)
  128.       return (BIT_NULL);
  129.  
  130.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  131.       return (BIT_NULL);
  132.  
  133.    result->data = map->data;
  134.    result->x0 = map->x0 + x;
  135.    result->y0 = map->y0 + y;
  136.    result->wide = wide;
  137.    result->high = high;
  138.    result->primary = map->primary;
  139.    result->type = map->type;
  140.    Bprintf(2) ("  Created %d,%d %d,%d\n", result->x0, result->y0,
  141.            result->wide, result->high);
  142.    return (result);
  143. }
  144.  
  145. /* allocate space for, and create a memory bitmap */
  146.  
  147. BITMAP *
  148. bit_alloc(wide, high, data, bits)
  149. unsigned short wide, high;
  150. DATA data;
  151. int bits;    /* in preparation for color */
  152. {
  153.    char *malloc();
  154.    register BITMAP *result;
  155.    register int size;
  156.  
  157.    Bprintf(1) ("bit_alloc:\n");
  158.    if ((result = (BITMAP *) malloc(sizeof(BITMAP))) == (BITMAP *) 0)
  159.       return (result);
  160.  
  161.    result->x0 = 0;
  162.    result->y0 = 0;
  163.    result->high = high;
  164.    result->wide = wide;
  165.  
  166.    size = BIT_SIZE(result);
  167.  
  168.    if (data != (DATA ) 0)
  169.       result->data = data;
  170. #ifdef ALIGN32
  171.    else if ((result->data = (DATA ) memalign(4,size)) == (DATA ) 0) {
  172. #else
  173.    else if ((result->data = (DATA ) malloc(size)) == (DATA ) 0) {
  174. #endif
  175.       free(result);
  176.       return ((BITMAP *) 0);
  177.    }
  178.  
  179.    result->primary = result;
  180.    result->type = _MEMORY;
  181.    Bprintf(2) ("  Created %d,%d %d,%d\n", result->x0, result->y0,
  182.            result->wide, result->high);
  183.    return (result);
  184. }
  185.